package com.ruoyi.settlement.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.settlement.domain.Lessee;
import com.ruoyi.settlement.domain.TbOrder;
import com.ruoyi.settlement.domain.Vo;
import com.ruoyi.settlement.utils.SnowFlake;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.settlement.domain.TbSettlement;
import com.ruoyi.settlement.service.ITbSettlementService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 结算Controller
 * 
 * @author ruoyi
 * @date 2023-05-08
 */
@RestController
@RequestMapping("/settlement")
@Component
public class TbSettlementController extends BaseController
{
    @Autowired
    private ITbSettlementService tbSettlementService;

    /**
     * 查询结算列表
     */
    @RequiresPermissions("settlement:settlement:list")
    @GetMapping("/list")
    public TableDataInfo list(TbSettlement tbSettlement)
    {
        startPage();
        List<TbSettlement> list = tbSettlementService.selectTbSettlementList(tbSettlement);
        return getDataTable(list);
    }

    /**
     * 导出结算列表
     */
    @RequiresPermissions("settlement:settlement:export")
    @Log(title = "结算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSettlement tbSettlement)
    {
        List<TbSettlement> list = tbSettlementService.selectTbSettlementList(tbSettlement);
        ExcelUtil<TbSettlement> util = new ExcelUtil<TbSettlement>(TbSettlement.class);
        util.exportExcel(response, list, "结算数据");
    }

    /**
     * 获取结算详细信息
     */
    @RequiresPermissions("settlement:settlement:query")
    @GetMapping(value = "/{settlementId}")
    public AjaxResult getInfo(@PathVariable("settlementId") Long settlementId)
    {
        return success(tbSettlementService.selectTbSettlementBySettlementId(settlementId));
    }

    /**
     * 新增结算
     */
    @RequiresPermissions("settlement:settlement:add")
    @Log(title = "结算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSettlement tbSettlement)
    {
        return toAjax(tbSettlementService.insertTbSettlement(tbSettlement));
    }

    /**
     * 修改结算
     */
    @RequiresPermissions("settlement:settlement:edit")
    @Log(title = "结算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSettlement tbSettlement)
    {
        return toAjax(tbSettlementService.updateTbSettlement(tbSettlement));
    }

    /**
     * 删除结算
     */
    @RequiresPermissions("settlement:settlement:remove")
    @Log(title = "结算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{settlementIds}")
    public AjaxResult remove(@PathVariable Long[] settlementIds)
    {
        return toAjax(tbSettlementService.deleteTbSettlementBySettlementIds(settlementIds));
    }

    //修改结算信息 发起申请 审批
    @GetMapping("/update/{sid}")
    public AjaxResult updateStatus(@PathVariable String sid){
        long id = Long.parseLong(sid);
        tbSettlementService.update(id);
        System.out.println("修改成功"+new Date());
        return new AjaxResult(200,"修改成功","111");
    }

    @Autowired
    private SnowFlake snowFlake;

    @XxlJob("timedSettelment1")
    public void timedSettelment1(){
        Vo vo=new Vo();
        //获取当前时间
        Date date=new Date();
        vo.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date).toString());
        long time = date.getTime() - 1000 * 60 * 60 * 24;
        Date date1=new Date();
        date1.setTime(time);
        vo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1).toString());

        //查询前一天的订单
        List<TbOrder> orderList = tbSettlementService.findOrderList(vo);
        for (TbOrder tbOrder : orderList) {
            TbSettlement tbSettlement=new TbSettlement();
            //雪花算法生成id
            tbSettlement.setSettlementId(snowFlake.getNextId());
            tbSettlement.setSettlementTime(new Date());
            tbSettlement.setPayType(1L);
            tbSettlement.setTotalPrice(tbOrder.getReal_price());
            tbSettlement.setOrderNum(1L);
            //计算平台服务费
            BigDecimal price1= tbOrder.getReal_price().multiply(BigDecimal.valueOf(0.0038)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            //计算支付服务费
            BigDecimal price2= tbOrder.getReal_price().multiply(BigDecimal.valueOf(0.0010)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            //计算可提现金额
            BigDecimal price3=tbOrder.getReal_price().subtract(price1).subtract(price2).setScale(2, BigDecimal.ROUND_HALF_EVEN);
            tbSettlement.setServiceCharge(price1);
            tbSettlement.setPayServiceFee(price2);
            tbSettlement.setWithdrawPrice(price3);
            //设置确认状态
            tbSettlement.setIsAck(0l);
            //设置是否提现
            tbSettlement.setIsWithdraw(0l);
            //设置提现申请时间
            tbSettlement.setWithdrawTime(new Date());
            //设置是否打款
            tbSettlement.setIsRemit(0l);
            //设置收款账号
            tbSettlement.setCollectionAccount("38123232355239552323");
            //生成流水号
            tbSettlement.setSerialNumber(snowFlake.getNextId()+"");
            tbSettlementService.insertTbSettlement(tbSettlement);
            System.out.println("结算了"+new Date());
        }

    }


    @XxlJob("timedSettelment2")
    public void timedSettelment(){
        Vo vo=new Vo();
        //获取当前时间
        Date date=new Date();
        vo.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date).toString());
        long time = date.getTime() - 1000 * 60 * 60 * 24;
        Date date1=new Date();
        date1.setTime(time);
        vo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1).toString());

        //判断付费模式
        List<Lessee> lesseeList = tbSettlementService.getPayBy();

        for (Lessee lessee : lesseeList) {
            if (lessee.getLessee_policy().equals(2)){
                //查询前一天的订单
                List<TbOrder> orderList = tbSettlementService.findOrderList(vo);
                for (TbOrder tbOrder : orderList) {
                    TbSettlement tbSettlement=new TbSettlement();
                    //雪花算法生成id
                    tbSettlement.setSettlementId(snowFlake.getNextId());
                    tbSettlement.setSettlementTime(new Date());
                    tbSettlement.setPayType(1L);
                    tbSettlement.setTotalPrice(tbOrder.getReal_price());
                    tbSettlement.setOrderNum(1L);
                    //计算平台服务费
                    BigDecimal price1= tbOrder.getReal_price().multiply(BigDecimal.valueOf(0.0038)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    //计算支付服务费
                    BigDecimal price2= tbOrder.getReal_price().multiply(BigDecimal.valueOf(0.0010)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    //计算可提现金额
                    BigDecimal price3=tbOrder.getReal_price().subtract(price1).subtract(price2).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    tbSettlement.setServiceCharge(price1);
                    tbSettlement.setPayServiceFee(price2);
                    tbSettlement.setWithdrawPrice(price3);
                    //设置确认状态
                    tbSettlement.setIsAck(0l);
                    //设置是否提现
                    tbSettlement.setIsWithdraw(0l);
                    //设置提现申请时间
                    tbSettlement.setWithdrawTime(new Date());
                    //设置是否打款
                    tbSettlement.setIsRemit(0l);
                    //设置收款账号
                    tbSettlement.setCollectionAccount("38123232355239552323");
                    //生成流水号
                    tbSettlement.setSerialNumber(snowFlake.getNextId()+"");
                    tbSettlementService.insertTbSettlement(tbSettlement);
                    System.out.println("手续费结算了"+new Date());
                }
            }else{
                //查询前一天的订单
                List<TbOrder> orderList = tbSettlementService.findOrderList(vo);
                for (TbOrder tbOrder : orderList) {
                    TbSettlement tbSettlement=new TbSettlement();
                    //雪花算法生成id
                    tbSettlement.setSettlementId(snowFlake.getNextId());
                    tbSettlement.setSettlementTime(new Date());
                    tbSettlement.setPayType(1L);
                    tbSettlement.setTotalPrice(tbOrder.getReal_price());
                    tbSettlement.setOrderNum(1L);
                    //不需要计算平台服务费

                    //计算支付服务费
                    BigDecimal price2= tbOrder.getReal_price().multiply(BigDecimal.valueOf(0.0010)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    //计算可提现金额
                    BigDecimal price3=tbOrder.getReal_price().subtract(price2).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    tbSettlement.setServiceCharge(BigDecimal.valueOf(0));
                    tbSettlement.setPayServiceFee(price2);
                    tbSettlement.setWithdrawPrice(price3);
                    //设置确认状态
                    tbSettlement.setIsAck(0l);
                    //设置是否提现
                    tbSettlement.setIsWithdraw(0l);
                    //设置提现申请时间
                    tbSettlement.setWithdrawTime(new Date());
                    //设置是否打款
                    tbSettlement.setIsRemit(0l);
                    //设置收款账号
                    tbSettlement.setCollectionAccount("38123232355239552323");
                    //生成流水号
                    tbSettlement.setSerialNumber(snowFlake.getNextId()+"");
                    tbSettlementService.insertTbSettlement(tbSettlement);
                    System.out.println("年费模式结算了"+new Date());
                }
            }
        }

    }
}
